package a.b.reggie.controller;

import a.b.reggie.common.R;
import a.b.reggie.pojo.ShoppingCart;
import a.b.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    /**
     * 获取购物车信息
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> getList(){
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    /**
     * 向购物车添加食品
     * @return
     */
    @PostMapping("/add")
    public R<String> addSetmeal(HttpServletRequest request, @RequestBody ShoppingCart shoppingCart){
        //获取手机号
        String phone = (String) request.getSession().getAttribute("user");
        shoppingCartService.addFood(phone,shoppingCart);
        return R.success("添加成功");
    }

    /**
     * 减少食品数量
     * @param map
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody Map map){
        log.info("id"+map.get("dishId"));
        Long setmealId = Long.valueOf(String.valueOf(map.get("dishId")));
        shoppingCartService.sub(setmealId);
        return R.success("减少成功");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
        shoppingCartService.remove(null);
        return R.success("清空成功");
    }


}
